import {createWebHashHistory,createRouter} from 'vue-router'

import Home from '../components/Home.vue'
import Login from '../components/Login.vue'


const route = createRouter({

    history:createWebHashHistory(),
    routes:[
        {
            path:'/',
            component:Home
        },
        {
            path:'/home',
            component:Home
        },
        {
            path:'/login',
            component:Login
        }
    ]
})

route.beforeEach((to, from, next) => {
    // to and from are both route objects. must call `next`.
    console.log(`从哪里来:${from.path},到哪里去:${to.path}`)
    if(to.path=='/login'){
        next()
    }else{
        let username = window.sessionStorage.getItem("username")
        if(null!=username){
            next()
        }else{
            next('/login')
        }
    }
})

// route.beforeEach((to,from,next)=>{
//     /* 
//     to 要去那
//     from 从哪里来
//     next 放行路由时需要调用的方法,不调用则不放行
//     */
//     console.log(`从哪里来:${from.path},到哪里去:${to.path}`)

//     if(to.path == '/login'){
//         //放行路由  注意放行不要形成循环  
//         next()
//     }else{
//         //let username =window.sessionStorage.getItem('username'); 
//         let username =window.sessionStorage.getItem('username'); 
//         if(null != username){
//             next()
//         }else{
//             next('/login')
//         }

//     }
// })


export default route